package com.pigs.springbootpigsvuecrud.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pigs.springbootpigsvuecrud.entity.PgUser;
import com.pigs.springbootpigsvuecrud.entity.model.ResultFormat;
import com.pigs.springbootpigsvuecrud.entity.model.ResultFormatPaging;
import com.pigs.springbootpigsvuecrud.service.PgUserService;
import com.pigs.springbootpigsvuecrud.utils.ResultPagingUtil;
import com.pigs.springbootpigsvuecrud.utils.ResultUtil;
import com.pigs.springbootpigsvuecrud.utils.TimeUtitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-05-19
 */
@RestController
@RequestMapping("/pgUser/")
public class PgUserController {

    @Autowired
    private PgUserService pgUserService;


    /**
     * 查询用户列表
     *
     * @return
     */
    @GetMapping("queryUserList")
    @CrossOrigin
    public ResultFormat queryUserList() {
        List<PgUser> pgUsersIPage = pgUserService.queryUserList();
        return ResultUtil.success(pgUsersIPage);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @PostMapping("delUser")
    @CrossOrigin
    public ResultFormat delUser(String id) {
        System.out.println(id);
        boolean removeById = pgUserService.removeById(id);
        System.out.println(removeById);
        if (removeById) {
            return ResultUtil.success(200, "删除成功");
        }
        return ResultUtil.error(100, "删除失败");

    }

    /**
     * 添加用户
     *
     * @param pgUser
     * @return
     */
    @PostMapping("saveUser")
    @CrossOrigin
    public ResultFormat saveUser(@RequestBody PgUser pgUser) {
        System.out.println(pgUser);
        pgUser.setUcreatetime(TimeUtitl.dateTime());
        boolean save = pgUserService.save(pgUser);
        System.out.println(save);
        if (save) {
            return ResultUtil.success(200, "添加成功");
        }
        return ResultUtil.error(100, "添加失败");
    }

    /**
     * 修改用户
     *
     * @param pgUser
     * @return
     */
    @PostMapping("editUser")
    @CrossOrigin
    public ResultFormat editUser(@RequestBody PgUser pgUser) {
        System.out.println(pgUser);
        boolean updateById = pgUserService.updateById(pgUser);
        System.out.println(updateById);
        if (updateById) {
            return ResultUtil.success(200, "修改成功");
        }
        return ResultUtil.error(100, "修改失败");
    }

}
